﻿//修改Bom

layui.use(['table', 'dropdown', 'layer', 'form'], function () {

    var table = layui.table;
    var dropdown = layui.dropdown;
    var layer = layui.layer;
    var form = layui.form;
    var laypage = layui.laypage;
    $(function () {
        Fan()
      //  QUyuan()
        Show()
   
    });
  
    window.Fan = function Fan() {
        var bombh = localStorage.getItem("bombh")
      $.ajax({
            url: '../Bom/BomBackfill',
            data: {
                Bombh: bombh,
                token: ""
            },
            type: 'Get',
            success: function (res) {
                var res = JSON.parse(res)
                console.log(res.data)
                localStorage.setItem("processRouteId", res.data.processRouteId)
                localStorage.setItem("bomid", res.data.bomid)
               Bombh = $("#Bombh").val()
                Bombanben = $("#Bombanben").val()
                Chenbh = $("#Chenbh").val()
                ChenName = $("#ChenName").val()
                Guige = $("#Guige").val()
                Chicun = $("#Chicun").val()
                CaozuoName = $("#CaozuoName").val()
                $("#Bombh").val(res.data.bombh)
                $("#Bombanben").val(res.data.bombanben)
                $("#Chenbh").val(res.data.chenbh)
                $("#ChenName").val(res.data.chenName)
                $("#Guige").val(res.data.guige)
                $("#Chicun").val(res.data.chicun)
                $("#CaozuoName").val(res.data.caozuoName)
     }
        })
        Fangong()
    }
    window.Show = function Show() {
         layui.use(function () {
           // 先进行表格的初始渲染
            table.render({
                elem: '#carriertable',  // 表格元素的选择器
                page: {
                    limit: 5,
                    limits: [5, 8, 10]
                },
                cols: [[
                    { type: 'checkbox', fixed: 'left' },
                    { field: 'ingredientnumber', title: '原料编号' },
                    { field: 'ingredientName', title: '原料名称' },

                    { field: 'specification', title: '规格型号' },
                    { field: 'size', title: '尺寸' },
                    { field: 'unit', title: '单位' },
                    {
                        field: '', width: 260, title: '操作', templet: function (d) {
                            return '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="FanUpdate(' + d.ingredientId + ')"><i class="layui-icon layui-icon-edit"></i>编辑</a>' +
                                '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="IsDel(' + d.ingredientId + ')"><i class="layui-icon layui-icon-delete"></i>删除</a>';

                        }
                    }
                ]],
                // 初始时可以先不加载数据，等待 ajax 请求成功后再更新
                data: []
            });
         // 创建表格实例
            $.ajax({
                url: "../Ingredient/IngredientShow",
                type: "get",
                data: {
                    Pageindex: 1,
                    Pagesize: 100,
                    token: "",
                    Ingredientnumber: "",
                    IngredientName: "",
                    State: ""
                },
           success: function (data) {
                    data = JSON.parse(data);
                    console.log(data.data.list);
                    // 在 ajax 请求成功后，使用正确的表格 ID 进行 reload
                    table.reload('carriertable', {
                        page: {
                            limit: 10,
                            limits: [5, 8, 10]
                        },
                        data: data.data.list
                    });
                },
                error: function () {
                    console.log("请求失败");
                }
            });
        });
    }
    window.Fangong = function Fangong() {
        var id = localStorage.getItem("processRouteId")
        if (id!=null) {
            layui.use(function () {
                // 先进行表格的初始渲染
                table.render({
                    elem: '#hh',  // 表格元素的选择器
                    page: {
                        limit: 5,
                        limits: [5, 8, 10]
                    },
                    cols: [[
                        { type: 'checkbox', fixed: 'left' },
                        { field: 'productionProcessesName', title: '工序名称' },
                        { field: 'productionProcessesNumber', title: '工序编号' },
                        { field: 'processClassification', title: '工序分类' },
                     
                    ]],
                    // 初始时可以先不加载数据，等待 ajax 请求成功后再更新
                    data: []
                });

                // 创建表格实例
                $.ajax({
                    url: "../Bom/BomGong",
                    type: "get",
                    data: {
                        ProcessRouteId: id,
                        token: ""
                    },
                    success: function (data) {
                        data = JSON.parse(data);
                        console.log(data.data);
                      
                        // 在 ajax 请求成功后，使用正确的表格 ID 进行 reload
                        table.reload('hh', {
                            page: {
                                limit: 5,
                                limits: [5, 8, 10]
                            },
                            data: [data.data]
                        });
                    },
                    error: function () {
                        console.log("请求失败");
                    }
                });

            });
        }
           
    }
    //修改
    window.BomUpdate = function BomUpdate() {
        var bomid = localStorage.getItem("bomid")
        console.log(bomid)
        Bombh = $("#Bombh").val()
        Bombanben = $("#Bombanben").val()
        Chenbh = $("#Chenbh").val()
        ChenName = $("#ChenName").val()
        Guige = $("#Guige").val()
        Chicun = $("#Chicun").val()
        CaozuoName = $("#CaozuoName").val()
     // 取原料项目id
        var checkStatus = table.checkStatus('carriertable');
        var selectedIds = checkStatus.data.map(function (item) { return item.ingredientId; });
        IngredientId = selectedIds
    
        ProcessRouteId = localStorage.getItem("processRouteId")
        console.log(ProcessRouteId)
          $.ajax({
              url: '../Bom/BomUpdate',
            type: 'post',
              data: {
                  bomid: bomid,
                Bombh: Bombh,
                Bombanben: Bombanben,
                Chenbh: Chenbh,
                Guige: Guige,
                ChenName: ChenName,
                Chicun: 727,
                CaozuoName: CaozuoName,
                IngredientId: IngredientId,
                ProcessRouteId: ProcessRouteId
            },
            //contentType: 'application/json',
            success: function (res) {
                console.log(res)
                var res = JSON.parse(res)
                if (res.data > 0) {
                    alert("修改成功")
                    window.location.href = '/Bom/BomIndex'
                }
                else {
                    alert("修改失败")
                }

            }
        })

    }
 
});



